package com.tueye.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.tueye.dao.ITopicTypeDao;
import com.tueye.entity.TopicType;
import com.tueye.service.ITopicTypeService;

@Scope("prototype")
@Component("topicTypeService")
public class TopicTypeServiceImpl implements ITopicTypeService {
	
	@Autowired
	@Qualifier("topicTypeDao")
	ITopicTypeDao topicTypeDao;
	
	@Override
	public String getData() {
		List<TopicType> topicTypeList = topicTypeDao.getParentData();
		return getTreeRoot(topicTypeList);
	}
	private String getTreeRoot(List<TopicType> topicTypeList)
	{
		String returStr = "[";
		for(TopicType topicType :topicTypeList)
		{
			returStr += "{ data :'" + clearStr(topicType.getType_name())+ "'," ;
			returStr += "attributes: {id:'" + clearStr(topicType.getId()) +"'},";
			returStr += getSubJoson(topicType);
			returStr += "state:'open'}," ;
		}
		returStr = returStr.substring(0,returStr.length()-1) + "]";
		System.out.println("returStr:" + returStr);
		return returStr;
	}
	private String getSubJoson(TopicType topicType)
	{
		List<TopicType> list = topicTypeDao.getDataByParentId(topicType.getId());
		String retunStr ="";
		if(list.size() >0 )
		{
			retunStr = "children : [ ";
			for(TopicType subTopicType :list){ 
				retunStr += "{data : '" + clearStr(subTopicType.getType_name())+ "',";
				retunStr += "attributes: {id:'" + clearStr(subTopicType.getId()) + "'},";
				retunStr += getSubJoson(subTopicType);
				retunStr += "state:'open'}," ;
			}
			retunStr = retunStr.substring(0,retunStr.length()-1) + "],";
		}
		return retunStr;
	}
	@Override
	public String addTopicType(String[] value) {
		return topicTypeDao.addTopicType(value);
	}
	@Override
	public String deleteTopicType(String[] value) {
		topicTypeDao.deleteTopicType(value);
		return null;
	}
	@Override
	public String updateTopicType(String[] value) {
		topicTypeDao.updateTopicType(value);
		return null;
	}
	private String clearStr(String str)
	{
		if(str!=null && str.startsWith("'"))
		{
			str=str.substring(1,str.length());
		}
		if(str!=null && str.endsWith("'"))
		{
			str=str.substring(0,str.length() - 1);
		}
		return str;
	}
	@Override
	public String getData(String id) {
		
		List<TopicType> topicTypeList =null;
		if(id == null || "0".equals(id))
		{
			topicTypeList = topicTypeDao.getParentData();
		}else
		{
			topicTypeList = topicTypeDao.getDataByParentId(id);
		}
		String returnStr = "";
		//开始拼串
		if(topicTypeList.size() > 0)
		{
			returnStr = "[";
			for(TopicType topicType :topicTypeList)
			{
				returnStr += "{ data :'" + clearStr(topicType.getType_name())+ "'," ;
				returnStr += "attributes: {id:'" + clearStr(topicType.getId()) +"'},";
				//returnStr += getSubJoson(topicType);
				returnStr += "state:'open'}," ;
			}
			returnStr = returnStr.substring(0,returnStr.length()-1) + "]";
		}
		System.out.println("returStr:" + returnStr);
		return returnStr;
	}
	@Override
	public String getNodeInforById(String id) {
		TopicType topicType = topicTypeDao.getNodeInfoById(id);
		String returnStr = "";		
		if(topicType!= null)
		{
			returnStr ="{" ;
			returnStr += "typeEnlishName:'" +topicType.getType_enlish_name()+"',";
			returnStr += "typeName:'" + topicType.getType_name() +"',";
			returnStr += "typePageDesc:'" + topicType.getType_page_description()+"',";
			returnStr += "typePageKey:'" + topicType.getType_page_keywords()+"',";
			returnStr += "typePageTitle:'" + topicType.getType_page_title() +"'}";
		}
		return returnStr;
	}
}

